/*
	file:StorageServer.cpp
	author:luoo
	date:2015 04 12
*/

#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <libxml/parser.h>
#include <string.h>
#include <stdlib.h>  
#include <errno.h>  
#include <netdb.h>
#include <unistd.h>
using namespace std;
#define MAXDATASIZE 4098
#define STORAGEPORT 8088
#define STORAGESERVERIP "127.0.0.1"

class StorageThread
{
	public:
		char buf[MAXDATASIZE];
		char *registeredstorage="";
		int snumbytes,snew_fd,ssockfd;
		struct sockaddr_in server_addr; 
        char * registermsg;
		StorageThread();
		~StorageThread();
		int InitConnection();
		char * SRD(char * buffer); //Send_Receive_Decode
};

StorageThread::StorageThread(){
    registermsg="<Envelope type=\"sregister\"></Envelope>";
}

StorageThread::~StorageThread(){
	close(snew_fd);
}

int StorageThread::InitConnection(){
    if ( ( ssockfd = socket ( AF_INET , SOCK_STREAM , 0)) == - 1) { 
        perror("socket error"); 
        return -1;
    } 
    memset ( &server_addr, 0, sizeof(struct sockaddr)); 
    server_addr. sin_family = AF_INET; 
    server_addr. sin_port = htons (STORAGEPORT); 
    server_addr. sin_addr. s_addr = inet_addr(STORAGESERVERIP); 
    if ( connect ( ssockfd, ( struct sockaddr * ) & server_addr, sizeof( struct sockaddr ) ) == -1) { 
        perror ("cannot connect to a storageserver..."); 
        return -1;
    } 
}

char * StorageThread::SRD(char * buffer){ 
    //send
    if ( send ( ssockfd, buffer , strlen(buffer), 0) == -1) { 
        perror ( "send error" ); 
        return NULL;
    }
    //recevice
    if ( ( snumbytes = recv ( ssockfd, buf, MAXDATASIZE, 0) ) == -1) { 
        perror( "recv error" ); 
        return NULL;
    } 
    if ( snumbytes) { 
        buf[snumbytes] = '\0'; 
        printf("received: %s\n",buf); 
    }
    return buf;
}
